
package middleware;

import kalender.backend.*;
import kalender.backend.*;

public class Utility {

    static String generateSession(Server server, int UID){
        Session session = new Session(UID);
	if (UID != 1) { // we never want to forcibly expire multiple default user sessions, yo!
	    deleteAllSessionWithUID(server, UID);
	}
        server.sessionArray.add(session);
        return session.getUUID();
    }

    static int getUid(Server server, String key){
        for(int i = 0; i < server.sessionArray.size(); i++){
            Session session = server.sessionArray.get(i);
            if(session.getUUID().equals(key)){
                if(session.getUID() < 0){// has expired
                    deleteOldSessions(server);
                }else{
                    session.refreshExpireDate();
                    return session.getUID();
                }
            }
        }
        return -1;
    }

    static void deleteOldSessions(Server server){
        for(int i = 0; i < server.sessionArray.size(); i++){
            if(server.sessionArray.get(i).hasExpired()){
                server.sessionArray.remove(i);
                deleteOldSessions(server);
                break;// kan inte fortsätta loopa, iom arrayen har krympt.
            }
        }
    }

    static void deleteAllSessionWithUID(Server server, int UID){
        for(int i = 0; i < server.sessionArray.size(); i++){
            if(server.sessionArray.get(i).getUID() == UID){
                server.sessionArray.remove(i);
                deleteAllSessionWithUID(server, UID);
                break;// kan inte fortsätta loopa, iom arrayen har krympt.
            }
        }
    }


    static boolean isAdmin(int Uid){
        Luser luser = null;
        try{
            luser = Luser.find(Uid);
        }catch(BackendException e){
            return false;
        }
        if(isAdmin(luser))
            return true;
        return false;
    }
    // TODO kanske lägga till en boolean i databasen?
    static boolean isAdmin(Luser luser){
        if(luser.getUname().equalsIgnoreCase("ADMIN"))
            return true;
        return false;
    }

    // TODO kanske lägga till en boolean i databasen?
    static boolean isDefault(int Uid){
        Luser luser = null;
        try{
            luser = Luser.find(Uid);
        }catch(BackendException e){
            return false;
        }
        if(luser.getUname().equalsIgnoreCase("DEFAULT"))
            return true;
        return false;
    }

    static boolean isNameReserved(String name){
        if(name.equalsIgnoreCase("ADMIN") || name.equalsIgnoreCase("DEFAULT"))
            return true;
        return false;
    }

    static String arrayToString(char[] c){
        return new String(c);
    }

    static int toInt(String str){
        try{
            return Integer.parseInt(str);
        }catch(NumberFormatException e){
        }
        return 0;
    }

    static boolean isAlphaNum(String s) {
        if (s.matches("[\\s0-9a-zA-ZåäöÅÄÖéÉ_]*")) {
            return true;
        } else {
            return false;
        }
    }

    static boolean toBoolean(String s){
        if(s.equalsIgnoreCase("true"))
            return true;
        return false;
    }


    static String getUserAgent(String str){
        if(str.contains("Firefox"))
            return "Firefox";
        if(str.contains("Chrome"))
            return "Chrome";
        if(str.contains("Apache-HttpClient"))// android? blir iallafall så med den virituella maskinen.
            return "Apache-HttpClient";
        if(str.contains(".NET"))// iexplorer?
            return ".NET";
        return "";
    }
    static String setEncoding(String str){
        /* Example:
         * Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
         *
         * pr("\n\nENCODING:");
         * pr(str);
         * pr("1:"+str.split(" ", 2)[1]);
         * pr("2:"+str.split(" ", 2)[1].split(";", 2)[0]);
         * pr("3:"+str.split(" ", 2)[1].split(";", 2)[0].split(","));
         * pr("4:"+str.split(" ", 2)[1].split(";", 2)[0].split(",")[0]);
         * pr("end of encoding\n\n");
         *
         */
        String[] encoding = str.split(" ", 2)[1].split(";", 2)[0].split(",");
        return encoding[0];
    }
}
